package com.crm.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.backend.domain.entity.RestBean;
import com.crm.backend.domain.po.SaleChance;
import com.crm.backend.service.ISaleChanceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-29
 */

@RestController
@RequestMapping("/api/v1/sale-chance")
public class SaleChanceController {

    @Autowired
    private ISaleChanceService saleChanceService;

    /**
     * 新增销售机会
     *
     * @param saleChance 销售机会信息
     * @return RestBean封装的响应结果
     */
    @PostMapping
    public RestBean<Boolean> addSaleChance(@RequestBody SaleChance saleChance) {
        boolean savedSaleChance = saleChanceService.save(saleChance);
        return savedSaleChance==true?RestBean.success():RestBean.failure(400,"新增失败");
    }

    /**
     * 修改销售机会
     *
     * @param id         销售机会编号
     * @param saleChance 修改后的销售机会信息
     * @return RestBean封装的响应结果
     */
    @PutMapping("/{id}")
    public RestBean<Void> updateSaleChance(@PathVariable(value = "id") Integer id, @RequestBody SaleChance saleChance) {
        saleChance.setSalId(id);
        boolean updatedSaleChance = saleChanceService.updateById(saleChance);
        if (updatedSaleChance == true) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "销售机会未找到");
        }
    }

    /**
     * 删除销售机会
     * @param id 销售机会编号
     * @return RestBean封装的响应结果
     */
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteSaleChance(@PathVariable(value = "id") Integer id) {
        boolean deleted = saleChanceService.removeById(id);
        if (deleted) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "销售机会未找到");
        }
    }

    /**
     * 指派销售机会
     * @param id 销售机会编号
     * @param assignee 指派人
     * @return RestBean封装的响应结果
     */
    @PostMapping("/assign")
    public RestBean<Void> assignSaleChance(@PathVariable(value = "id")Integer id, @RequestParam(value = "assignee") String assignee) {
        SaleChance saleChance = saleChanceService.getById(id);
        if (saleChance != null) {
            saleChance.setSalAssign(assignee);
            saleChance.setSalState(2); // 已分配状态
            boolean updatedSaleChance = saleChanceService.updateById(saleChance);
            if(updatedSaleChance){
                return RestBean.success();
            }
            return RestBean.failure(400,"更新指派失败");
        } else {
            return RestBean.failure(404, "销售机会未找到");
        }
    }


    /**
     * 分页查询销售机会
     * @param page 当前页码
     * @param size 每页显示条数
     * @param salName 客户名称（模糊查询条件，可选）
     * @param salSource 机会来源（模糊查询条件，可选）
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public RestBean<IPage<SaleChance>> pageSaleChance(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "size") Integer size,
            @RequestParam(value = "salName",required = false) String salName,
            @RequestParam(value = "salSource",required = false) String salSource) {
        // 创建分页对象
        Page<SaleChance> pages = new Page<>(page, size);

        // 创建 QueryWrapper 对象用于构建查询条件
        QueryWrapper<SaleChance> queryWrapper = new QueryWrapper<>();
        // 添加模糊查询条件（如果参数不为空）
        if (StringUtils.isNotBlank(salName)) {
            queryWrapper.like("sal_name", salName);
        }
        if (StringUtils.isNotBlank(salSource)) {
            queryWrapper.like("sal_source", salSource);
        }

        // 调用 MyBatis-Plus 的分页查询方法
        IPage<SaleChance> result = saleChanceService.page(pages, queryWrapper);

        // 返回分页查询结果
        return RestBean.success(result);
    }
}